package Str;

import java.util.Scanner;

/**
 * @author Hongliang Zhu
 * @create 2021-08-05 23:47
 */
public class HJ39_ip {

    private static final int ILLEGAL_IP = 1;
    private static final int EQUAL = 0;
    private static final int NOT_EQUAL = 2;



    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            String mask = sc.nextLine();
            String ip1 = sc.nextLine();
            String ip2 = sc.nextLine();

            if(checkIp(ip1) && checkIp(ip2) && checkMask(mask)){
                if(isBlongs(ip1, ip2, mask)){
                    System.out.println(EQUAL);
                }else{
                    System.out.println(NOT_EQUAL);
                }

            }else {
                System.out.println(ILLEGAL_IP);
            }
        }
    }

    private static boolean isBlongs(String ip1, String ip2, String mask) {
        // 转成Int
        int ip1_int = ipToInteger(ip1);
        int ip2_int = ipToInteger(ip2);
        int mask_int = ipToInteger(mask);
        int res1 = ip1_int & mask_int;
        int res2 = ip2_int & mask_int;

        return res1 == res2;
    }

    public static int ipToInteger(String ip){
        String[] ips = ip.split("\\.");
        int ipFour = 0;
        //因为每个位置最大255，刚好在2进制里表示8位
        for(String ip4: ips){
            Integer ip4a = Integer.parseInt(ip4);
            //这里应该用+也可以,但是位运算更快
            ipFour = (ipFour << 8) | ip4a;
        }
        return ipFour;
    }

    /**
     * 判断ip是否合法：
     * 有效掩码与IP的性质为：
     * 1. 掩码与IP每一段在 0 - 255 之间
     * 2. 掩码的二进制字符串前缀为网络号，都由‘1’组成；后缀为主机号，都由'0'组成
     * @param ip
     * @return
     */
    private static boolean checkIp(String ip) {

        String[] ips = ip.split("\\.");
        for (int i = 0; i < ips.length; i++) {
            int ip_value = Integer.parseInt(ips[i]);
            if(ip_value < 0 ||  ip_value >255){
                return false;
            }
        }
        return true;
    }

    private static boolean checkMask1(String mask){
        String[] ips = mask.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ips.length; i++) {
            int ip_value = Integer.parseInt(ips[i]);
            if(ip_value < 0 ||  ip_value >255){
                return false;
            }
            String bi = Integer.toBinaryString(ip_value);
            if (bi.length() < 8){
                int len = bi.length();
                // 补0
                for (int k = 0;  k <= 8 - len ;k++){
                    bi += "0";
                }
            }
            sb.append(bi);
        }

        String s = sb.toString();

        int index = s.lastIndexOf("1");
        // 1 前面不能有0， 1 后面只能有0
        for (int i = 0; i <= index; i++){
            if(s.charAt(i) == '0'){
                return false;
            }

        }

        for (int i = index + 1; i< s.length(); i++){
            if(s.charAt(i) == '1'){
                return false;
            }
        }

        return true;
    }

    /**
     * 判断子网掩码
     *
     * 判断掩码非法
     * m + (m & (-m)) || !(m + 1)
     * @param mask
     * @return
     */
    private static boolean checkMask(String mask){

        if(!checkIp(mask)){
            return false;
        }

        int mm = ipToInteger(mask);

//        long mm = Long.parseLong(mask);
        if((mm | (mm-1)) == 0xffffffff){
            return true;
        }else {
            return false;
        }


    }




}
